/*
 * This file is part of FEMJava.
 *
 * last modified: September 20, 10:00 AM
 *
 */
package br.ufmg.dees.insane.ui.rich.learn.learn_view.equation_view.command;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

import br.ufmg.dees.insane.ui.rich.learn.learn_view.LearnInterface;



/**
 * A class defining a html viewer with vertical scroll bar.
 *
 * @author Pitangueira, Roque & Caldas, Krishna  
 * @version 0.0.1 Beta  
 * @since   jdk1.2.2
 */
public class HtmlDA extends JPanel implements HyperlinkListener {

	private static final long serialVersionUID = 1L;

	/**
     * The editor panel
     */
    private JEditorPane html;

    /**
     * An initial URL.
     */
    private URL url;

    /**
     * The parent of editor.
     */
    private LearnInterface parent;

    /**
     * The constructor method
     *
     * @param     parent   the parent of the class.
     * @see       femjava.client.Interface
     */
    public HtmlDA(LearnInterface parent) {
        this.parent = parent;
        initComponents();
    } // End of Constructor
    
    private void initComponents() {
        this.setLayout(new BorderLayout());
        Dimension d = new Dimension(300,500);
        html = new JEditorPane();
        html.setSize(d);
        
        //Creating scroll pane and adding editor pane
        JScrollPane paneScrollPane = new JScrollPane(html);

        //Defining the policy statement to scroll pane
        paneScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        paneScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

        html.setContentType("text/html");
        html.setEditable(false);
        html.setFont(new Font("Arial", Font.PLAIN, 10));

        //Adding scroll pane to draw area center pane
        
        
        this.setSize(d);
        this.setPreferredSize(d);
        this.add(paneScrollPane, BorderLayout.CENTER);
       
        
    }

    /**
     * Method required by HyperlinkListener interface.
     * Implements a listener to hyperlinks.
     *
     * @param      e An Hyperlink event, ex: click .
     * @see        javax.swing.JEditorPane#setPage(URL url)
     * On exception:
     * @see        femjava.client.Interface#setMessage(String str)
     */
    public void hyperlinkUpdate(HyperlinkEvent e) {
        if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
            try {
                html.setPage(e.getURL());
            } catch (IOException ioe) {
                parent.setMessage("Erro em:" + e.toString());
            }
        }
    }

    /**
     * Set html viewer to a URL
     *
     * @param     s    the URL string
     */
    public void goTo(String s) {

        try {
            url = new URL(s);
            html.setPage(url);
        } catch (MalformedURLException e) {
            parent.setMessage(e.toString());
        } catch (IOException ioe) {
            parent.setMessage("IOException" + ioe);
        }
    } //End of goTo
    
    /**
     * Sets the html viewr text
     *
     * @param     s    the desired string of text
     */
    public void setText(String s) {
        html.setText(s);
    }//End of setText
    
    public JEditorPane getHtml(){
    	return html;
    }
    
    public void setVisible(boolean valor) {
    	html.setVisible(valor);
    }

} //End of class


//==== End of file